<template>
<div class="box" >
    <div class="add" @click="add()">新增</div>
  <div>
      <van-search
      class="info_search"
      v-model="value"
      placeholder="搜索"
      @search="onSearch"
      @clear="clearSearch"
    />
  </div>
  <list-view @childEmit='childEmit($event)' :uData="users"></list-view>
</div>
</template>

<script>
import { Search } from 'vant';
import ListView from '../../components/ListView.vue'
import { GetUserDic } from '@/api/userUser'
export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      value: "",
      users:[],
    };
  },
  components: {
    ListView
  },
  created() {
    this.getUsers();
  },
  methods:{
    add() {
      this.$router.push({
        path: "/UserinfoAdd",
        query: { id: 0 },
      });
    },
    getUsers(){
      this.users=[];
      let conditions=[];
      if (this.value) {
        conditions.push({
          Name: "Keywords",
          DisplayName: this.value,
          Value: this.value,
          Operator: "like",
          DataType: "String",
        });
      }
      let params = {conditions:JSON.stringify(conditions)};
      GetUserDic(params).then((res)=>{
        // console.log(res);
        // if(res && res.data){
        //   for(var i in res.data){
        //     if(res.data[i].length>0){
        //       var myData={
        //         title: i,
        //         items:[],
        //       }
        //       for(var j=0;j<res.data[i].length;j++){
        //         var itemData = {
        //             name:res.data[i][j].ChnName,
        //             LastWord:res.data[i][j].LastWord,
        //             IsAccounted:res.data[i][j].IsAccounted,
        //             roleNames:[],
        //             orgName:res.data[i][j].OrgName==null?'':res.data[i][j].OrgName,
        //             userName:res.data[i][j].UserName
        //         }
        //         if(res.data[i][j].RoleName){
        //           var roleNameArr = res.data[i][j].RoleName.split(',')
        //           if(roleNameArr.length>0){
        //             var maxRoleCnt = 3;
        //             if(roleNameArr.length<maxRoleCnt){
        //               maxRoleCnt = roleNameArr.length
        //             }
        //             for(var m=0;m<maxRoleCnt;m++){
        //               itemData.roleNames.push(roleNameArr[m])
        //             }
        //           }
        //         }
        //         myData.items.push(itemData)
        //       }
        //       this.users.push(myData)
        //     }
        //   }
        // }
        let letterArr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        for(var p = 0; p< letterArr.length;p++) {
          this.users.push({
            title:letterArr[p],
            items:[]
          })
        }
        if(res && res.data){
          for(var i in res.data){
            var newArr = this.users.filter(item => item.title==i);
            if(newArr && newArr.length>0){
              var myData = newArr[0]
              if(res.data[i] && res.data[i].length>0){
                  for(var j=0;j<res.data[i].length;j++){
                    var itemData = {
                      name:res.data[i][j].ChnName,
                      LastWord:res.data[i][j].LastWord,
                      IsAccounted:res.data[i][j].IsAccounted,
                      roleNames:[],
                      orgName:res.data[i][j].OrgName==null?'':res.data[i][j].OrgName,
                      userName:res.data[i][j].UserName
                  }
                  if(res.data[i][j].RoleName){
                    var roleNameArr = res.data[i][j].RoleName.split(',')
                    if(roleNameArr.length>0){
                      var maxRoleCnt = 3;
                      if(roleNameArr.length<maxRoleCnt){
                        maxRoleCnt = roleNameArr.length
                      }
                      for(var m=0;m<maxRoleCnt;m++){
                        itemData.roleNames.push(roleNameArr[m])
                      }
                    }
                  }
                    myData.items.push(itemData)
                }
              }
            }
          }
        }
      })
    },
    childEmit(data){
      if(data && data.UserName){
        this.$router.push({
          path: "/UserDetail",
          query: { userName: data.UserName },
        });
      }
    },
    onSearch(val){
      this.value = val
      this.getUsers()
    },
    clearSearch() {
      this.value = '';
      this.getUsers()
    },
  },
}
</script>
<style lang="less" scoped>
.add{
  z-index: 9999;
}
</style>